﻿namespace AddressBook.Web
{
	#region "Using Statements"
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;

	using Business.Person;
	using Business.Contact;
	using Business.Event;
	#endregion

	public partial class ManageEvents : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.Page.IsPostBack)
			{
				this.BindLocationData();
				this.BindEventList();
			}
		}

		#region "Bind Functions"

		private void BindEventList()
		{
			EventList events = EventList.GetEventList();
			this.ManageEventsEventList.DataSource = events;
			this.ManageEventsEventList.DataBind();
		}

		private void BindGuests(Event eventData)
		{
			this.EditEventGuestList.DataSource = eventData.Guests;
			this.EditEventGuestList.DataBind();

			ContactList contacts = ContactList.GetContactList();

			this.ContactListBox.DataSource = contacts.Where(c => !eventData.Guests.Contains(c.Id, GuestType.Contact)).OrderBy(c => c.FirstName);
			this.ContactListBox.DataTextField = "Name";
			this.ContactListBox.DataValueField = "Id";
			this.ContactListBox.DataBind();

			FamilyList families = FamilyList.GetFamilyList();

			this.FamilyListBox.DataSource = families.Where(f => !eventData.Guests.Contains(f.Id, GuestType.Family)).OrderBy(f => f.Name);
			this.FamilyListBox.DataTextField = "Name";
			this.FamilyListBox.DataValueField = "Id";
			this.FamilyListBox.DataBind();
		}

		private void BindLocationData()
		{
			LocationList locations = LocationList.GetLocationList();

			this.AddEventLocationDropDown.DataSource = locations;
			this.AddEventLocationDropDown.DataValueField = "Id";
			this.AddEventLocationDropDown.DataTextField = "Name";
			this.AddEventLocationDropDown.DataBind();

			this.EditEventLocationDropDown.DataSource = locations;
			this.EditEventLocationDropDown.DataValueField = "Id";
			this.EditEventLocationDropDown.DataTextField = "Name";
			this.EditEventLocationDropDown.DataBind();
		}

		#endregion

		#region "Add Event Events"

		protected void AddEventSaveButton_Click(object sender, ImageClickEventArgs e)
		{
			Event eventData = Event.NewEvent();

			eventData.Name = this.AddEventNameTextBox.Text;
			eventData.LocationId = int.Parse(this.AddEventLocationDropDown.SelectedValue);


			eventData = eventData.Save();
			this.BindEventList();

			this.AddEventNameTextBox.Text = string.Empty;
			this.AddEventLocationDropDown.SelectedIndex = -1;
		}

		#endregion

		#region "Event List Events"

		protected void ManageEventsEventList_ItemCommand(object sender, ListViewCommandEventArgs e)
		{
			switch (e.CommandName)
			{
				case "Edit":
					Event eventData = Event.GetEvent(int.Parse(e.CommandArgument.ToString()));

					this.EditEventDetailsPanel.Visible = true;
					this.EditEventNameLiteral.Text = eventData.Name;
					this.EditEventLocationLiteral.Text = Location.GetLocation(eventData.LocationId).Name;
					this.EditEventIdField.Value = eventData.Id.ToString();

					this.BindGuests(eventData);
					break;

				case "Delete":
					Event.DeleteEvent(int.Parse(e.CommandArgument.ToString()));
					this.BindEventList();
					break;
			}
		}

		protected void ManageEventsEventList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
		{

		}

		protected void ManageEventsEventList_ItemEditing(object sender, ListViewEditEventArgs e)
		{

		}

		#endregion

		#region "Event Events"

		protected void EditEventCloseButton_Click(object sender, ImageClickEventArgs e)
		{
			this.EditEventDetailsPanel.Visible = false;
			this.EditEventNameLiteral.Text = string.Empty;
			this.EditEventLocationLiteral.Text = string.Empty;
			this.EditEventIdField.Value = string.Empty;
		}

		protected void AddGuestSaveButton_Click(object sender, ImageClickEventArgs e)
		{
			Event eventData = Event.GetEvent(int.Parse(this.EditEventIdField.Value));

			foreach (ListItem item in ContactListBox.Items)
			{
				if (item.Selected == true)
				{
					eventData.Guests.Add(int.Parse(item.Value), GuestType.Contact);
				}
			}

			foreach (ListItem item in FamilyListBox.Items)
			{
				if (item.Selected == true)
				{
					eventData.Guests.Add(int.Parse(item.Value), GuestType.Family);
				}
			}

			eventData = eventData.Save();
			this.BindGuests(eventData);
		}

		protected void EditGuestList_ItemCommand(object sender, ListViewCommandEventArgs e)
		{
			switch (e.CommandName)
			{
				case "Delete":
					string[] values = e.CommandArgument.ToString().Split(',');

					Event eventData = Event.GetEvent(int.Parse(this.EditEventIdField.Value));
					eventData.Guests.Remove(int.Parse(values[0]), (GuestType)int.Parse(values[1]));
					eventData = eventData.Save();

					this.BindGuests(eventData);
					break;
			}
		}

		protected void EditGuestList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
		{

		}

		protected void EditEventDetailsButton_Click(object sender, ImageClickEventArgs e)
		{
			Event eventData = Event.GetEvent(int.Parse(this.EditEventIdField.Value));
			this.EditEventNameTextBox.Text = eventData.Name;
			this.EditEventLocationDropDown.SelectedValue = eventData.LocationId.ToString();

			this.EditEventPanelPopupExtender.Show();
		}

		protected void EditEventSaveButton_Click(object sender, ImageClickEventArgs e)
		{
			Event eventData = Event.GetEvent(int.Parse(this.EditEventIdField.Value));

			eventData.Name = this.EditEventNameTextBox.Text;
			eventData.LocationId = int.Parse(this.EditEventLocationDropDown.SelectedValue);

			this.EditEventNameLiteral.Text = eventData.Name;
			this.EditEventLocationLiteral.Text = Location.GetLocation(eventData.LocationId).Name;

			eventData = eventData.Save();

			this.BindEventList();

			this.EditEventNameTextBox.Text = string.Empty;
			this.EditEventLocationLiteral.Text = string.Empty;
		}

		#endregion
	}
}
